// DataQueryHandler.cpp: implementation of the DataQueryHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "DataQueryHandler.h"
#include "CoalMineUnit.h"
#include "Server.h"

#include "CommonLib\Structure.h"
#include "CommonLib\Packet.h"
#include "Connection.h"
#include "ConnectionManager.h"

#include "CommServerLib\CommWithClient.h"

#include "MonitoringUnitLib\BaseMonitoringUnit.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

DataQueryHandler::DataQueryHandler(const Packet& packet, HANDLE hClient):
	BaseRequestHandler(packet, hClient)
{
}

DataQueryHandler::~DataQueryHandler()
{
}

bool DataQueryHandler::Prehandling()
{
	DataQueryStruct * pDataQueryStruct = (DataQueryStruct *)m_pPacket->GetpData();

	DataAckStruct * pDataAckStruct = NULL;

	if (pDataQueryStruct->m_nCoalMineId == 0)
	{
		if (pDataQueryStruct->m_dwNumOfItems >0)
		{
			DWORD dwBufSize = sizeof(DataAckStruct)+sizeof(MonUnitDataItem)*(pDataQueryStruct->m_dwNumOfItems-1);
			pDataAckStruct = (DataAckStruct *)new BYTE[dwBufSize];

			for (int i = 0;i < pDataQueryStruct->m_dwNumOfItems;i++)
			{
				MonUnitItem * pMonUnitItem = &pDataQueryStruct->m_MonUnitItems[i];
				MonUnitDataItem * pMonUnitDataItem = &pDataAckStruct->m_DataItems[i];

				pMonUnitDataItem->m_nCoalMineId = pMonUnitItem->m_nCoalMineId;
				strcpy(pMonUnitDataItem->m_MonUnitData.m_szId, pMonUnitItem->m_szMonUnitId);

				bool bSuccess = false;
				BaseCoalMineUnit * pCoalMineUnit = Server::Instance()->GetCoalMineUnitById(pMonUnitItem->m_nCoalMineId);
				if (pCoalMineUnit)
				{
					BaseMonitoringUnit * pMonitoringUnit = pCoalMineUnit->GetMonitoringUnitById(pMonUnitItem->m_szMonUnitId);
					if (pMonitoringUnit)
					{
						pMonitoringUnit->GetDataTime().GetAsSystemTime(pMonUnitDataItem->m_MonUnitData.m_stData);
						pMonitoringUnit->GetStateChangeTime().GetAsSystemTime(pMonUnitDataItem->m_MonUnitData.m_stStateChange);
						pMonUnitDataItem->m_MonUnitData.m_nState = pMonitoringUnit->GetState();
						pMonUnitDataItem->m_MonUnitData.m_fValue = pMonitoringUnit->GetValue();
						bSuccess = true;
					}
				}

				pMonUnitDataItem->m_bSuccess = bSuccess;
			}

			pDataAckStruct->m_dwBufSize = dwBufSize;
			pDataAckStruct->m_nCoalMineId = 0;
			pDataAckStruct->m_dwNumOfItems = pDataQueryStruct->m_dwNumOfItems;
		}
		else
		{
			pDataAckStruct = new DataAckStruct;
			pDataAckStruct->m_dwBufSize = sizeof(DataAckStruct);
			pDataAckStruct->m_nCoalMineId = pDataQueryStruct->m_nCoalMineId;
			pDataAckStruct->m_dwNumOfItems = 0;
		}
	}
	else
	{
		BaseCoalMineUnit * pCoalMineUnit = Server::Instance()->GetCoalMineUnitById(pDataQueryStruct->m_nCoalMineId);
		MyConnection * pConnection = ConnectionManager::Instance()->GetConnectionByHandle(m_hClient);
		int nId = pDataQueryStruct->m_nCoalMineId;
		if(pCoalMineUnit && pConnection->m_nIdList[nId]==nId ){
			//if(pCoalMineUnit){
			const std::vector<BaseMonitoringUnit *>& MonitoringUnits = pCoalMineUnit->GetMonitoringUnits();

			if (MonitoringUnits.size() > 0)
			{
				DWORD dwBufSize = sizeof(DataAckStruct)+sizeof(MonUnitDataItem)*(MonitoringUnits.size()-1);
				pDataAckStruct = (DataAckStruct *)new BYTE[dwBufSize];

				for (int i = 0;i < MonitoringUnits.size();i++)
				{
					BaseMonitoringUnit * pMonitoringUnit = MonitoringUnits[i];
					MonUnitDataItem * pMonUnitDataItem = &pDataAckStruct->m_DataItems[i];
					pMonUnitDataItem->m_nCoalMineId = pDataQueryStruct->m_nCoalMineId;
					ASSERT(sizeof(pMonUnitDataItem->m_MonUnitData.m_szId) > pMonitoringUnit->GetId().GetLength());
					strcpy(pMonUnitDataItem->m_MonUnitData.m_szId, pMonitoringUnit->GetId());
					pMonUnitDataItem->m_MonUnitData.m_nState = pMonitoringUnit->GetState();
					pMonUnitDataItem->m_MonUnitData.m_fValue = pMonitoringUnit->GetValue();
					pMonitoringUnit->GetDataTime().GetAsSystemTime(pMonUnitDataItem->m_MonUnitData.m_stData);
					pMonitoringUnit->GetStateChangeTime().GetAsSystemTime(pMonUnitDataItem->m_MonUnitData.m_stStateChange);
					pMonUnitDataItem->m_bSuccess = true;
				}

				pDataAckStruct->m_dwBufSize = dwBufSize;
				pDataAckStruct->m_nCoalMineId = pDataQueryStruct->m_nCoalMineId;
				pDataAckStruct->m_dwNumOfItems = MonitoringUnits.size();
			}
		}
		if(!pDataAckStruct)
		{
			pDataAckStruct = new DataAckStruct;
			pDataAckStruct->m_dwBufSize = sizeof(DataAckStruct);
			pDataAckStruct->m_nCoalMineId = pDataQueryStruct->m_nCoalMineId;
			pDataAckStruct->m_dwNumOfItems = 0;
		}
	}

	pDataAckStruct->m_dwMethodType = VCS_MT_QUERYDATA;
	pDataAckStruct->m_nMethodAck = VCS_MR_OK;

	Packet packetAck(pDataAckStruct->m_dwBufSize, (BYTE *)pDataAckStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	delete pDataAckStruct;

	return true;
}